$(function () {

    $("#jqGrid").jqGrid({
        url: baseURL + 'machine/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', width: 30, key: true},
            {label: '门店id', name: 'storeId', index: "store_id", width: 40},
            {label: '名称', name: 'name', width: 15},
            {
                label: '打印类型',
                name: 'printType',
                index: "print_type",
                width: 20,
                formatter: function (value, options, row) {
                    if (value == 1) {
                        return '<span class="label label-danger">后厨打印</span>';
                    } else if (value == 2) {
                        return '<span class="label label-info">前台打印</span>';
                    } else if (value == 3) {
                        return '<span class="label label-success">排队打印</span>';
                    } else {
                        return "";
                    }
                }
            },
            {label: '机器号', name: 'machineNum', index: "machine_num", width: 40},
            {label: '打印数量', name: 'printNum', index: "print_num", width: 40},
            {label: '打印机状态', name: 'printStatusInfo', index: "print_status_info", width: 40},
            {
                label: '使用状态',
                name: 'useStatus',
                index: "use_status",
                width: 40,
                formatter: function (value, options, row) {
                    if (value == 1) {
                        return '<span class="label label-success">正常</span>';
                    } else if (value == 2) {
                        return '<span class="label label-danger">禁用</span>';
                    } else {
                        return "";
                    }
                }
            },
        ],
        viewrecords: true,
        height: 720,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        },
    });

});

var vm = new Vue({
    el: '#dcapp',
    data: {
        q: {
            name: null,
        },
        id: null,
        showList: true,
        title: null,
        printer: {},
    },
    created: function () {

    },
    methods: {
        query: function () {
            vm.reload();
        },
        update: function () {
            var id = getSelectedRow();//选择一条
            if (id == null) {
                return;
            }
            vm.getPrinter(id);
            vm.showList = false;
            vm.showDetal = false;
            vm.title = "修改打印机";
        },
        add: function () {
            vm.printer = {};
            vm.showList = false;
            vm.title = "添加打印机";
        },
        saveOrUpdate: function () {
            var url = vm.printer.id == null ? "machine/add" : "machine/update";
            var formJson = JSON.stringify(vm.printer);
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: formJson,
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function () {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (i) {
            vm.printer = {};
            var id = getSelectedRow();//选择一条
            if (id == null) {
                return;
            }
            vm.printer.id = id;
            vm.printer.useStatus = i
            vm.saveOrUpdate();
        },
        getPrinter: function (id) {
            $.ajax({
                type: "GET",
                url: baseURL + "machine/info/" + id,
                success: function (r) {
                    vm.printer = r.printer;
                }
            });

        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'key': vm.q.key},
                page: page
            }).trigger("reloadGrid");
        },
    }
});